package com.bird;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 司军强
 * @Date: 2020/3/19 23:14
 * @Description: 自定义框架实现 jsp
 * webServlet web容器的映射路径
 */
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    //将当前类注入到logger工厂里面

    Logger logger = LoggerFactory.getLogger(HelloServlet.class);


    @Override
    public void init() {
        System.out.println("当第一次访问时,初始化servlet ....");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("是我执行了doGet方法，我才是入口");
        doGet(request, response);
    }


    /**
     * <P>httpServlet 真正执行的get方法</P>
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = "司军强的简易框架";
        //打印debug级别的日志  test git exercise
        logger.debug("print logger debug={}", name);
        //往作用域添加值
        request.setAttribute("name", name);
        //将请求转发到-->jsp
        request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("当项目关闭时候 会执行这个destroy()的方法....");
    }


    /**
     * ##=========================servlet方法执行的流程==================================##
     * 1：当第一次访问的时候,先执行init()方法
     * 2：然后再执行servlet()方法里面doGet() 或者doPost()方法
     * 3:当项目关闭的是最后再执行destory()方法
     * */

}
